<template>
    <div class="layout">
        <div class="title">朋友圈互动趋势</div>
        <div class="btn">
            <el-radio-group v-model="currentIndex">
                <el-radio-button label="1">点赞数</el-radio-button>
                <el-radio-button label="2">评论数</el-radio-button>
                <el-radio-button label="3">评论人数</el-radio-button>
            </el-radio-group>
            <div v-auth="'MOMENTS_EXP_BOARD'" class="btn_right" @click="export2Img(myRef, '朋友圈互动趋势')">导出</div>
        </div>
        <div v-show="!isNoMessage" ref="myRef" class="line"></div>
        <div v-show="isNoMessage" class="no_data">
            <img class="no_img" src="@/assets/noData.png" alt="" />
            <div class="emty_title">暂无数据</div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, inject, nextTick, watch } from 'vue';
import { export2Img } from '@/utils/utils';
const echarts = inject('ec');
const currentIndex = ref(1);
const myRef = ref(null);
const isNoMessage = ref(false);
const props = defineProps(['chartData', 'timeData']);
const emit = defineEmits({
    changeId: null
});
// 切换浏览的数据
const change = (event: any) => {
    currentIndex.value = event.target.id;
    emit('changeId', currentIndex.value);
};
const baseData = ref([]);
// 素材
const getChart = (data: any) => {
    if (data.length == 0) {
        isNoMessage.value = true;
        return;
    } else {
        isNoMessage.value = false;
    }
    const myChart = echarts.init(myRef.value);
    myChart.setOption({
        grid: {
            left: 40,
            right: 40,
            top: 30,
            bottom: 40,
            containLabel: true
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#FF6B00'
                }
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: props.timeData
        },
        yAxis: {
            type: 'value',
            splitNumber: 10,
            containLabel: true
        },
        series: [
            {
                name: currentIndex.value == 1 ? '点赞数' : currentIndex.value == 2 ? '评论数' : '评论人数',
                data: data,
                type: 'line',
                smooth: true,
                symbol: 'circle',
                symbolSize: 0,
                areaStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#F8DCD1' },
                            { offset: 0.5, color: '#FDF8F6' },
                            { offset: 1, color: '#FFFAF9' }
                        ])
                    }
                },
                itemStyle: {
                    normal: {
                        borderWidth: 2,
                        color: '#FF9D23'
                    }
                },
                emphasis: {
                    // 鼠标 hover 时的样式
                    itemStyle: {
                        color: '#FF9D23', // hover 圆点颜色
                        borderColor: '#FF9D23', // hover 圆点边框颜色
                        borderWidth: 8
                    }
                }
            }
        ]
    });
    nextTick(() => {
        myChart.resize();
    });
    window.onresize = function () {
        myChart.resize();
    };
};

watch(
    () => props.chartData,
    val => {
        getChart(val);
    }
);

watch(currentIndex, v => {
    currentIndex.value = v;
    emit('changeId', v);
});
</script>

<style lang="scss" scoped>
.layout {
    height: 546px;
    padding: 16px;
    box-sizing: border-box;
    border: 1px solid #eaeaea;
    background-color: #ffff;
    margin-top: 16px;

    .title {
        width: 100%;
        padding-bottom: 17px;
        border-bottom: 1px solid #eaeaea;
    }

    .btn {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 21px;

        .btn_left {
            display: flex;
            align-items: center;

            .btn_item {
                padding: 10px 18px;
                background: #fff;
                font-size: 16px;
                line-height: 16px;
                font-weight: 500;
                color: #ff6b00;
                border: 1px solid #ff6b00;
                cursor: pointer;
            }

            .active {
                background: #ff6b00;
                color: #fff;
            }
        }

        .btn_right {
            padding: 11px 18px;
            font-size: 14px;
            font-weight: 300;
            background: #fff;
            border-radius: 4px;
            border: 1px solid #eaeaea;
            cursor: pointer;
        }
    }

    .line {
        // width: 83vw;
        height: 450px;
    }

    .no_data {
        height: 500px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        .no_img {
            margin-left: 35px;
        }

        .emty_title {
            color: #909399;
            margin: 0;
        }
    }
}
</style>
